package com.tensquare.user.service;

import java.util.*;
import java.util.concurrent.TimeUnit;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.servlet.http.HttpServletRequest;

import com.rabbitmq.tools.json.JSONUtil;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import javafx.scene.transform.Shear;
import jdk.nashorn.internal.parser.JSONParser;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import util.IdWorker;

import com.tensquare.user.dao.UserDao;
import com.tensquare.user.pojo.User;
import util.JwtUtil;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
@Transactional
public class UserService {

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private IdWorker idWorker;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private BCryptPasswordEncoder encoder;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private JwtUtil jwtUtil;

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<User> findAll() {
		return userDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<User> findSearch(Map whereMap, int page, int size) {
		Specification<User> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return userDao.findAll(specification, pageRequest);
	}


	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<User> findSearch(Map whereMap) {
		Specification<User> specification = createSpecification(whereMap);
		return userDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public User findById(String id) {
		return userDao.findById(id).get();
	}
	public 	User findByPhone(String phone) {

		return userDao.findByMobile(phone);


	}

	/**
	 * 增加
	 * @param user
	 */
	public String add(User user) {
		String uid =  idWorker.nextId()+"";
		//请求头信息
		String url = "http://tensquare-chat/chat/register";
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("Content-Type","application/json");
		Map<String,String> map = new HashMap<>();
		map.put("username",uid);
		try {
			HttpEntity<String> httpEntity = new HttpEntity(map,httpHeaders);
			//发起请求
			ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
			System.out.println(responseEntity.getBody());
			if(responseEntity.getBody().equals("fail")){
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		user.setId(uid);
		//密码加密
		user.setPassword(encoder.encode(user.getPassword()));
		user.setFollowcount(0);//关注数
		user.setFanscount(0);//粉丝数
		user.setOnline(0L);//在线时长
		user.setRegdate(new Date());//注册日期
		user.setUpdatedate(new Date());//更新日期
		user.setLastdate(new Date());//最后登陆日期
		userDao.save(user);
		return uid;
	}

	/**
	 * 修改
	 * @param user
	 */
	public void update(User user) {
		user.setPassword(encoder.encode(user.getPassword()));
		user.setUpdatedate(new Date());//更新日期
		userDao.save(user);
	}
	public void updateStatus(User user) {

		userDao.updateStatus(user.getStatus(),user.getId());
	}
	public void updateUserinfo(User user) {
		user.setUpdatedate(new Date());//更新日期
		userDao.updateUserinfo(user.getNickname(),user.getAvatar(),user.getSex(),user.getPersonality(),user.getBirthday(),user.getInterest(),user.getAddress(),user.getUpdatedate(),user.getId());
	}

	/**
	 * 删除 必须有admin角色
	 * @param id
	 */
	public void deleteById(String id) {
//		String token  = (String) request.getAttribute("claims_asmin");
//		if(token == null || "".equals(token)){
//			throw new RuntimeException("权限不足！");
//		}
		userDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<User> createSpecification(Map searchMap) {

		return new Specification<User>() {

			@Override
			public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 手机号码
                if (searchMap.get("mobile")!=null && !"".equals(searchMap.get("mobile"))) {
                	predicateList.add(cb.like(root.get("mobile").as(String.class), "%"+(String)searchMap.get("mobile")+"%"));
                }
                // 密码
                if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
                	predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
                }
                // 用户名
                if (searchMap.get("username")!=null && !"".equals(searchMap.get("username"))) {
                	predicateList.add(cb.like(root.get("username").as(String.class), "%"+(String)searchMap.get("username")+"%"));
                }
                // 性别
                if (searchMap.get("sex")!=null && !"".equals(searchMap.get("sex"))) {
                	predicateList.add(cb.like(root.get("sex").as(String.class), "%"+(String)searchMap.get("sex")+"%"));
                }
                // 头像
                if (searchMap.get("avatar")!=null && !"".equals(searchMap.get("avatar"))) {
                	predicateList.add(cb.like(root.get("avatar").as(String.class), "%"+(String)searchMap.get("avatar")+"%"));
                }
                // E-Mail
                if (searchMap.get("email")!=null && !"".equals(searchMap.get("email"))) {
                	predicateList.add(cb.like(root.get("email").as(String.class), "%"+(String)searchMap.get("email")+"%"));
                }
                // 兴趣
                if (searchMap.get("interest")!=null && !"".equals(searchMap.get("interest"))) {
                	predicateList.add(cb.like(root.get("interest").as(String.class), "%"+(String)searchMap.get("interest")+"%"));
                }
                // 个性
                if (searchMap.get("personality")!=null && !"".equals(searchMap.get("personality"))) {
                	predicateList.add(cb.like(root.get("personality").as(String.class), "%"+(String)searchMap.get("personality")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

    public void sendSms(String mobile) {

		//生成6位数字随机数
		String checkCode = RandomStringUtils.randomNumeric(6);
		//向缓存中放一份
		redisTemplate.opsForValue().set("checkCode_"+mobile,checkCode,65, TimeUnit.SECONDS);
		Map<String,String> map = new HashMap<>();
		map.put("mobile",mobile);
		map.put("checkCode",checkCode);
		//给用户发一份
		rabbitTemplate.convertAndSend("sms",map);
		//在控制台显示一份【方便测试】
		System.out.println("验证码为："+checkCode);


    }

	public User login(String username, String password) {

		User user = userDao.findByUsername(username);
		if(user!=null && encoder.matches(password,user.getPassword())){

			return user;

		}

		return null;
	}

	@Transactional
    public void updateFansCountAndFollowCount(int x, String userid, String friendid) {

		userDao.updatefanscount(x,friendid);
		userDao.updatefollwcount(x,userid);

    }

	public String addByPhone(User user) {
		String uid =  idWorker.nextId()+"";
		//请求头信息
		String url = "http://tensquare-chat/chat/register";
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("Content-Type","application/json");
		Map<String,String> map = new HashMap<>();
		map.put("username",uid);
		try {
			HttpEntity<String> httpEntity = new HttpEntity(map,httpHeaders);
			//发起请求
			ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
			System.out.println(responseEntity.getBody());
			if(responseEntity.getBody().equals("fail")){
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		user.setId(uid);
		user.setFollowcount(0);//关注数
		user.setFanscount(0);//粉丝数
		user.setOnline(0L);//在线时长
		user.setRegdate(new Date());//注册日期
		user.setUpdatedate(new Date());//更新日期
		user.setLastdate(new Date());//最后登陆日期
		userDao.save(user);
		return uid;
	}

	public User findByUsername(String username) {
		return userDao.findByUsername(username);
	}
}
